<template>
  <div>
    <Sticky :class-name="'sub-navbar'">
      <el-button v-loading="loading" type="success" @click="addUser">
        保存信息
      </el-button>
    </Sticky>
    <h2>个人信息</h2>
    <el-form :model="form">
      <el-form-item label="账号" :label-width="formLabelWidth">
        <el-input v-model="form.name" autocomplete="off" disabled />
      </el-form-item>
      <el-form-item label="昵称" :label-width="formLabelWidth">
        <el-input v-model="form.nickName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="真名" :label-width="formLabelWidth">
        <el-input v-model="form.realName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="性别" :label-width="formLabelWidth">
        <el-radio v-model="form.sex" :label="0">保密</el-radio>
        <el-radio v-model="form.sex" :label="1">男</el-radio>
        <el-radio v-model="form.sex" :label="2">女</el-radio>
      </el-form-item>
      <el-form-item label="头像" :label-width="formLabelWidth">
        <el-upload
          class="avatar-uploader"
          action="#"
          :http-request="UploadImage"
          :show-file-list="false"
          accept="image/jpeg,image/png,image/jpg"
          :on-success="handleAvatarSuccess"
        >
          <img
            v-if="form.avatar"
            :src="form.avatar"
            class="avatar"
          >
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
      </el-form-item>
      <el-form-item label="介绍" :label-width="formLabelWidth">
        <el-input v-model="form.introduction" autocomplete="off" />
      </el-form-item>
      <el-form-item label="邮箱" :label-width="formLabelWidth">
        <el-input v-model="form.email" autocomplete="off" />
      </el-form-item>
      <el-form-item label="手机" :label-width="formLabelWidth">
        <el-input v-model="form.mobile" autocomplete="off" />
      </el-form-item>
      <el-form-item label="权限" :label-width="formLabelWidth">
        <el-radio v-model="form.authority" :label="0" disabled>管理员</el-radio>
        <el-radio v-model="form.authority" :label="1" disabled>教师</el-radio>
        <el-radio v-model="form.authority" :label="2" disabled>学生</el-radio>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Sticky from '@/components/Sticky'
import { UploadImage } from '@/api/course'
import { getInfoSelf, saveUser } from '@/api/user'
export default {
  components: {
    Sticky
  },
  data() {
    return {
      formLabelWidth: '120px',
      form: {
        id: '',
        name: '',
        nickName: '',
        realName: '',
        sex: '',
        avatar: '',
        introduction: '',
        email: '',
        mobile: '',
        authority: ''
      },
      loading: false
    }
  },
  beforeCreate() {
    getInfoSelf().then(response => {
      console.log(response.data)
      this.form = response.data
    })
  },
  methods: {
    UploadImage(param) {
      const formData = new FormData()
      formData.append('file', param.file)
      UploadImage(formData).then(response => {
        console.log('上传图片成功')
        param.onSuccess() // 上传成功的图片会显示绿色的对勾
        this.form.avatar = response.msg
        // 但是我们上传成功了图片， fileList 里面的值却没有改变，还好有on-change指令可以使用
      }).catch(response => {
        console.log(response.msg)
        console.log('图片上传失败')
        param.onError()
      })
    },
    handleAvatarSuccess(res, file) {
      // 图片上传，成功返回图片地址
      this.form.avatar = URL.createObjectURL(file.raw)
    },
    addUser() {
      saveUser(this.form).then(response => {
        this.loading = false
        this.$notify({
          title: '成功',
          message: response.msg,
          type: 'success',
          duration: 2000
        })
      }).catch(() => {
        this.loading = false
      })
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
